package tyk.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tyk.pojo.Blog;
import tyk.pojo.User;
import tyk.service.BlogService;
import tyk.service.UserService;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/*
* 用户相关controller
* */
@Controller
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    /*
    * 用于显示管理页面
    * */
    @GetMapping
    public String index(Model model) {

        model.addAttribute("blogList", blogService.getAllBlog());
        return "adminIndex";
    }

    /*
    * 用于显示登录页面
    * */
    @GetMapping("/login")
    public String login() {

        return "adminLogin";
    }


    /*
    * 用于处理登录请求
    * */
    @PostMapping("/doLogin")
    public String doLogin(Model model, HttpSession session, String username, String password) {

        User user = userService.getUserByName(username);

        if (user == null) {

            model.addAttribute("msg","用户名不存在！");
            return "adminLogin";
        } else if (user.getPassWord().equals(password)) {

            session.setAttribute("username",username);
            return "redirect:/admin";
        } else {

            model.addAttribute("msg","用户名或者密码错误！");
            return "adminLogin";
        }
    }

    /*
    * 用于显示博客文章添加页面
    * */
    @GetMapping("/add")
    public String addBlog() {

        return "adminAdd";
    }

    /*
    * 用于处理添加博客文章页面提交的表单数据，添加博客文章
    * */
    @PostMapping("/doAdd")
    public String doAddBlog(Model model, @ModelAttribute Blog blog) {

        System.out.println(blog);

        LocalDateTime currentTime = LocalDateTime.now();
        Timestamp createDate = new Timestamp(currentTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
        blog.setCreateDate(createDate);
        System.out.println("---------------------------");
        int i = blogService.addBlog(blog);
        System.out.println("---------------------------");
        if (i>0){
            // List<Blog> blogList = blogService.selectBlog();
            // model.addAttribute("blogList", blogList);
            return "redirect:/admin";
        }else {
            model.addAttribute("msg", "添加失败");
            return "/admin/add";
        }
    }




    /*
     * 用于展示文章编辑页面
     * */
    @GetMapping("/edit/{blog_id}")
    public String showEditBlogByBlogId(Model model, @PathVariable Integer blog_id) {

        model.addAttribute("blog",blogService.getBlogById(blog_id));
        System.out.println("-----------------");
        System.out.println(blogService.getBlogById(blog_id));
        System.out.println("-----------------");
        return "adminEdit";
    }

    /*
     * 用于做修改文章方法
     * */
    @PostMapping("/doEdit")
    public String doEdit(Model model, @ModelAttribute Blog blog) {

        int count = blogService.doEdit(blog);
        if (count > 0) {

            return "redirect:/admin";
        } else {

            model.addAttribute("msg","修改失败");
            return "redirect:/admin/edit/"+blog.getId();
        }
    }


    /*
    * 用于删除文章
    * */
    @GetMapping("/delete")
    public String delete(Model model, Integer id) {

        int count = blogService.delete(id);
        if (count >0) {

            model.addAttribute("msg","删除成功");
            return "forward:/admin";
        } else {

            model.addAttribute("msg","删除失败");
            return "forward:/admin";
        }
    }
}
